<?php
class SalepurposeController  extends AdminBaseController{
	public function lists (){
		$data = HttpServerManager::getSalepurposeServer()->pagingBySearch($_GET);
		$now_year = date('Y');
		$before_year = $now_year-1;
		$next_year = $now_year+1;
		$year_pair = array($before_year=>$before_year.'年',$now_year=>$now_year.'年',$next_year=>$next_year.'年');
		$month_pair = array('1'=>'1月','2'=>'2月','3'=>'3月','4'=>'4月','5'=>'5月','6'=>'6月','7'=>'7月','8'=>'8月','9'=>'9月','10'=>'10月','11'=>'11月','12'=>'12月');
		
		$where = array();
		$where['pay_status']=PAY_STATUS_2;
		$completeOrderAmountList =  HttpDaoManager::getOrderDao()->getSumOrderAmountGroupByYearmonth($where);

		$completeRegisterList =  HttpDaoManager::getUserDao()->getSumRegisterGroupByYearmonth($where);
		
		$completOrderPair = array();
		foreach($completeOrderAmountList as $key=>$value){
			$completOrderPair[$value['add_time']]= $value['sumamount'];
		}
		
		$registerPair = array();
		$registerPayPair = array();
		foreach($completeRegisterList as $key=>$value){
			if(isset($registerPair[$value['register_time']])){
				$registerPair[$value['register_time']] +=$value['cnt'];
			}else{
				$registerPair[$value['register_time']]=$value['cnt']==""?0:$value['cnt'];
			}
			if($value['user_type']==USER_TYPE_MONEY){
				if(isset($registerPayPair[$value['register_time']])){
					$registerPayPair[$value['register_time']]+=$value['cnt'];
				}else{
					$registerPayPair[$value['register_time']]=$value['cnt']==""?0:$value['cnt'];
				}
			}
		}
		
		
		
		foreach($data['list'] as $key=>$value){
			$month = HttpServerManager::getSalepurposeServer()->filterMonth($value['month']);
			$data['list'][$key]['completeOrderAmount']=$completOrderPair[$value['year'].'-'.$month]; 	
			$data['list'][$key]['completeRegisterUser']=$registerPair[$value['year'].'-'.$month]; 	
			$data['list'][$key]['completeRegisterPayUser']=$registerPayPair[$value['year'].'-'.$month]; 	
		}

		$data['yearOptions'] =   page_modifier_options($year_pair,getParamValue('year'));
		$data['monthOptions'] =   page_modifier_options($month_pair,getParamValue('month'));
		return $data;
	}
	
	public function add(){
		$data = array();
		$now_year = date('Y');
		$next_year = $now_year+1;
		$year_pair = array($now_year=>$now_year.'年',$next_year=>$next_year.'年');
		
		$data['month'] = date('m')+1;
		$data['year']=date('Y');
 		$month_pair = array('1'=>'1月','2'=>'2月','3'=>'3月','4'=>'4月','5'=>'5月','6'=>'6月','7'=>'7月','8'=>'8月','9'=>'9月','10'=>'10月','11'=>'11月','12'=>'12月');
 		
		if (!empty($_GET['id'])){
			$data = HttpDaoManager::getSalepurposeDao()->get($_GET['id']);
		}
		
		$data['yearOptions'] =   page_modifier_options($year_pair,common::getStrValue($data, 'year'));
		$data['monthOptions'] =   page_modifier_options($month_pair,common::getStrValue($data, 'month'));	
		return $data;
	}

	public function subAdd(){
		$data = $_POST;
		$data['add_time'] = time();
		$data['add_user'] = $_SESSION['admin']['name'];
		
		$where = array();
		$where['year']=$_POST['year'];
		$where['month']=$_POST['month'];
		$salepurposeList = HttpDaoManager::getSalepurposeDao()->searchList($where);
		if(empty($salepurposeList)){
			HttpDaoManager::getSalepurposeDao()->add($data);
			ClassManager::getToos()->msgbox(Common::_t('data_insert_success'));
		}else{
			ClassManager::getToos()->msgbox('该月计划已存在，不能重复添加');
		}
	}
	
	public function subUpdate(){
		HttpServerManager::getSalepurposeServer()->update($_POST, $_POST['id']);
		ClassManager::getToos()->msgbox(Common::_t('data_edit_success'),'salepurpose_lists.html');
	}
	
	public function delete(){
		HttpServerManager::getSalepurposeServer()->deleteBatchByid($_GET['id']);
		ClassManager::getToos()->msgbox(Common::_t('data_del_success'));
	}	
}
?>